package unit6;

class Sleeper extends Thread {
    public Sleeper(String name) {
        super(name);
    }
    public void run() {
        try {
            sleep(1000);
        } catch (InterruptedException e) {
            System.out.println(getName() + "被中断。");
            return;
        }
        System.out.println(getName() + "休眠时间结束。");
    }
}
class Joiner extends Thread {
    private Sleeper sleeper;
    public Joiner(String name, Sleeper sleeper) {
        super(name);
        this.sleeper = sleeper;
    }
    public void run() {
        try {
            sleeper.join();//等待sleeper线程终止
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("加入线程" + getName() + "结束。");
    }
}
public class Ex6_2_Joining {
    public static void main(String[] args) {
        Sleeper s1 = new Sleeper("Sleeper1"), s2 = new Sleeper("Sleeper2");
        Joiner j1 = new Joiner("Joiner1", s1), j2 = new Joiner("Joiner2", s2);
        s1.start();
        s2.start();
        j1.start();
        j2.start();
//中断s2线程
        //s2.interrupt();
    }
}
